package com.lab.webSocket.repository;

import com.lab.webSocket.dto.ChatLogDto;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/9/7.
 */
public class ChatLogRepositoryImpl {

    @PersistenceContext
    private EntityManager em;

    public List<ChatLogDto> getLastChatLog(String userAccount) throws Exception {
        String sql = "select c.*,userNickname,userIcon from tb_chatLog c,tb_user where userAccount =senderAccount and " +
                "recipientAccount=? order by userAccount,sendTime ";
        Query query = em.createNativeQuery(sql).setParameter(1, userAccount);
        query.unwrap(NativeQuery.class).setResultTransformer(Transformers.aliasToBean(ChatLogDto.class));
        return query.getResultList();
    }
}
